#!/bin/sh
set -e 
basePath="/usr/local/consul"
ip=`ip -4 addr show eth0 | grep -oP "(?<=inet ).*(?=/)"`
ui=""
if [ "$1" = "ui" ]
then
    echo "Start With UI"
    ui="-ui"
fi

if [ ! -d "$basePath" ]
then
    echo "Create Data Dir: $basePath"
    sudo mkdir $basePath
fi

sudo $basePath/consul agent -config-file=$basePath/config.json -config-dir=$basePath/consul.d -bind=$ip -client=0.0.0.0 $ui &

echo "Consul Start Success."